/* 
* File:   DirectInputEnvironment.cpp
* Author: Ben
* 
* Created on May 1, 2013, 6:12 PM
*/

#pragma comment(lib, "dinput8.lib")
#pragma comment(lib, "dxguid.lib")

#include "DirectInputEnvironment.h"

#include <dinput.h>

namespace {
	bool setup = false;
	std::vector<controllers::Joystick> controllersVec;

	LPDIRECTINPUT8 directInput;

	HINSTANCE hInstance = (HINSTANCE) GetModuleHandle(NULL);
	BOOL CALLBACK addJoystick(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef){
		controllersVec.push_back(controllers::Joystick(directInput, lpddi));
		return DIENUM_CONTINUE;
	}
	void createDI(){
		if (directInput == NULL) {
			DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (LPVOID*) &directInput, NULL);
		}
	}
	void updateJoysticks() {
		createDI();
		directInput->EnumDevices(DI8DEVCLASS_ALL, (LPDIENUMDEVICESCALLBACK) addJoystick, NULL, DIEDFL_ALLDEVICES);
	}
}

namespace controllers {

	std::vector<controllers::Joystick>* getJoysticks() {
		if (controllersVec.size() == 0) {
			updateJoysticks();
		}
		return &controllersVec;
	}
}
